package com.wm.blog_gateway.config;

import com.wm.blog_common.constatnt.ApiMsg;
import com.wm.blog_common.exception.InvalidAccessTokenException;
import com.wm.blog_common.result.Result;
import com.wm.blog_gateway.exception.NoAuthException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
import org.springframework.cloud.gateway.support.NotFoundException;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.codec.HttpMessageReader;
import org.springframework.http.codec.HttpMessageWriter;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.util.Assert;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.reactive.result.view.ViewResolver;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Collections;
import java.util.List;

/***
 * @ClassName: GatewayExceptionHandler
 * @Description: 自定义网关异常   统一异常处理,参考  org.springframework.web.server.AbstractErrorWebExceptionHandler 修改
 * @Author: 半卷流年
 * @Create_time: 16:21 2020-3-28
 */
@Slf4j
public class GatewayExceptionHandler implements ErrorWebExceptionHandler  {

    /**
     * MessageReader
     */
    private List<HttpMessageReader<?>> messageReaders = Collections.emptyList();

    /**
     * MessageWriter
     */
    private List<HttpMessageWriter<?>> messageWriters = Collections.emptyList();

    /**
     * ViewResolvers
     */
    private List<ViewResolver> viewResolvers = Collections.emptyList();

    /**
     * 存储处理异常后的信息
     */
    private ThreadLocal<Result<?>> exceptionHandlerResult = new ThreadLocal<>();

    public void setMessageReaders(List<HttpMessageReader<?>> messageReaders) {
        Assert.notNull(messageReaders, "'messageReaders' must not be null");
        this.messageReaders = messageReaders;
    }

    public void setViewResolvers(List<ViewResolver> viewResolvers) {
        this.viewResolvers = viewResolvers;
    }

    public void setMessageWriters(List<HttpMessageWriter<?>> messageWriters) {
        Assert.notNull(messageWriters, "'messageWriters' must not be null");
        this.messageWriters = messageWriters;
    }

    /**
     * 处理逻辑
     *
     * @param exchange exchange
     * @param ex       ex
     * @return Mono
     */
    @Override
    public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
        String msg = "服务繁忙,请稍后重试....";
        // 返回给前端用的状态码
        int keyCode = ApiMsg.KEY_UNKNOWN;
        if (ex instanceof NotFoundException) {
            // 服务不可用
            keyCode = ApiMsg.KEY_SERVICE;
        } else if (ex instanceof ResponseStatusException) {
            ResponseStatusException responseStatusException = (ResponseStatusException) ex;
            msg = responseStatusException.getMessage();
            // 服务响应错误
            keyCode = ApiMsg.KEY_SERVICE;
        }else if(ex instanceof InvalidAccessTokenException){
            msg = ex.getMessage();
            // token非法
            keyCode = ApiMsg.KEY_TOKEN;
        }else if(ex instanceof NoAuthException){
            msg = ex.getMessage();
            keyCode = ApiMsg.KEY_TOKEN;
        }
        //todo 更多自定义异常,个性化定制返回信息

        // 封装响应体
        Result<String> responseBean = new Result<>(keyCode,msg);
        // 错误记录
        ServerHttpRequest request = exchange.getRequest();
        log.error("GatewayExceptionHandler: {}, error: {}", request.getPath(), ex.getMessage());
        if (exchange.getResponse().isCommitted()){
            return Mono.error(ex);
        }
        exceptionHandlerResult.set(responseBean);
        ServerRequest newRequest = ServerRequest.create(exchange, this.messageReaders);
        return RouterFunctions.route(RequestPredicates.all(), this::renderErrorResponse).route(newRequest)
                .switchIfEmpty(Mono.error(ex))
                .flatMap((handler) -> handler.handle(newRequest))
                .flatMap((response) -> write(exchange, response));
    }

    private Mono<ServerResponse> renderErrorResponse(ServerRequest request) {
        Result<?> responseBean = exceptionHandlerResult.get();
        //取出后释放threadLocal
        exceptionHandlerResult.remove();
        return ServerResponse.status(HttpStatus.OK)
                .contentType(MediaType.APPLICATION_JSON)
                .body(BodyInserters.fromValue(responseBean));
    }

    private Mono<? extends Void> write(ServerWebExchange exchange,
                                       ServerResponse response) {
        exchange.getResponse().getHeaders().setContentType(response.headers().getContentType());
        return response.writeTo(exchange, new ResponseContext());
    }

    private class ResponseContext implements ServerResponse.Context {

        @Override
        public List<HttpMessageWriter<?>> messageWriters() {
            return GatewayExceptionHandler.this.messageWriters;
        }

        @Override
        public List<ViewResolver> viewResolvers() {
            return GatewayExceptionHandler.this.viewResolvers;
        }
    }
}
